perf: avoid Optional.ofNullable wrapping on hook.before() return value#1955
perf: avoid Optional.ofNullable wrapping on hook.before() return value#1955tobias-ibounig-dt wants to merge 1 commit into
Conversation
Signed-off-by: Tobias Ibounig <tobias.ibounig@dynatrace.com>
|
There was a problem hiding this comment.
Code Review
This pull request simplifies the retrieval and validation of returnedEvalContext in HookSupport.java by directly assigning the result of hook.before and checking for null and presence. The reviewer suggested refactoring the code to use ifPresent instead of the isPresent() and get() pattern to make it more idiomatic and readable.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| .orElse(Optional.empty()); | ||
| if (returnedEvalContext.isPresent()) { | ||
| Optional<EvaluationContext> returnedEvalContext = hook.before(hookContext, data.getHints()); | ||
| if (returnedEvalContext != null && returnedEvalContext.isPresent()) { |
There was a problem hiding this comment.
While this condition is correct, you could make the code more idiomatic and avoid the isPresent()/get() pattern by using ifPresent.
For example:
if (returnedEvalContext != null) {
returnedEvalContext.ifPresent(returnedContext -> {
// existing logic from inside the if block
});
}This separates the null-check (to handle non-standard hook implementations) from the optional-handling, which can improve readability.
There was a problem hiding this comment.
ifPresent would use new Lambda or Method reference




This PR
Optional.ofNullable(hook.before(...)).orElse(Optional.empty())with a direct null checkRelated Issues
None
Notes
The previous pattern wrapped the return value of
hook.before()inOptional.ofNullable()to guard againstnullreturns (which are non-standard but possible). This created up to two temporaryOptionalobjects per hook call. The replacement assigns the return value directly and guards with an explicit!= nullcheck, which is semantically equivalent.Allocation impact is negligible, but reduces on totalAllocatedInstances can be seen.
main(baseline)run:+totalAllocatedBytesrun:+totalAllocatedInstancesFollow-up Tasks
-More PRs with memory improvements